package com.shoutao.website.controller.datacontroller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shoutao.website.contact.EditProductResponse;
import com.shoutao.website.contact.ShowTrainSignResponse;
import com.shoutao.website.contact.TrainSignRequest;
import com.shoutao.website.entity.TrainUser;
import com.shoutao.website.mapper.TrainUserMapper;
import com.shoutao.website.utils.ObjectUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.List;

/**
* @Description:
* @Param:
* @return:
* @Author: cqin
* @Date: 2019/11/5
*/
@RestController
@RequestMapping(value = "/api/train")
public class TrainController {


    @Autowired
    private TrainUserMapper trainUserMapper;


    @ApiOperation(value = "签到列表", notes = "签到列表")
    @RequestMapping(value = "/searchSign.do", method = RequestMethod.POST)
    public ShowTrainSignResponse searchSign(HttpServletRequest httpServletRequest) throws SQLException {

        ShowTrainSignResponse response = new ShowTrainSignResponse();
        List<TrainUser> queryResult = trainUserMapper.selectList(null);
        response.setTrainUsers(queryResult);
        return response;
    }


    @ApiOperation(value = "签到接口", notes = "签到接口")
    @RequestMapping(value = "/sign.do", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "request", value = "请求", dataType = "TrainSignRequest", required = true)
    })
    public EditProductResponse sign(HttpServletRequest httpServletRequest, TrainSignRequest request) throws SQLException {


        EditProductResponse response = new EditProductResponse();

        if(ObjectUtils.isNullOrEmpty(request.getUserName())){
            response.setResult(false);
            response.setResultMessage("请填写名字");
            return response;
        }


        TrainUser querySample = new TrainUser();
        querySample.setUsername(request.getUserName());
        List<TrainUser> queryResult = trainUserMapper.selectList(new QueryWrapper<>(querySample));
        if(!ObjectUtils.isNullOrEmpty(queryResult)) {
            response.setResult(false);
            response.setResultMessage("请不要重复签到哦");
            return response;
        }


        TrainUser trainUser = new TrainUser();
        trainUser.setUsername(request.getUserName());
        trainUserMapper.insert(trainUser);

        response.setResult(true);
        response.setResultMessage("签到成功");
        return response;

    }



}
